#include <iostream>
#include <functional>

using std::cout;
using std::endl;
using std::bind;
using std::function;

int add(int x, int y)
{
    cout << "int add(int, int)" << endl;
    return x + y;
}

class Example
{
public:
    int add(int x, int y)
    {
        cout << "int Example::add(int, int)" << endl;
        return x + y;
    }
};

void test()
{
    //函数的类型（标签）:函数的返回类型 + 函数的参数列表
    //bind可以改变函数的类型
    //int(int, int)---->int()
    /* auto f = bind(add, 1, 3); */
    //function是函数的容器，因为可以存放函数类型
    function<int()> f = bind(add, 1, 3);
    cout << "f() = " << f() << endl;

    cout << endl;
    Example ex;
    //int(Example *, int, int)--->int()
    function<int()> f3 = bind(&Example::add, &ex, 30, 40);
    cout << "f3() = " << f3() << endl;

    cout << endl;
    using namespace std::placeholders;
    //int(int, int)---->int(int, int)
    function<int(int, int)> f4 = bind(add, _2, _1);
    cout << "f4(10, 20) = " << f4(10, 20) << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}


